<?php
// +----------------------------------------------------------------------
// | ThinkPHP [ WE CAN DO IT JUST THINK ]
// +----------------------------------------------------------------------
// | Copyright (c) 2006~2017 http://thinkphp.cn All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: liu21st <liu21st@gmail.com>
// +----------------------------------------------------------------------

namespace app\admin\controller;
use think\Session;
use app\admin\controller\Base;
use app\admin\model\SupplierModel;
use app\admin\model\SupplierRoleModel;
use app\admin\model\FieldModel;
use app\admin\model\CountryModel;
use app\admin\model\SeriesModel;
use app\admin\model\MakeModel;
use app\admin\model\PtypeModel;

class Supplier extends Base
{
	/**
	 * 领域对象
	 * @return mixed
	 */
	public $supplier_obj;
	
	
	/**
	 * 领域对象
	 * @return mixed
	 */
	public $country_obj;
	
	/**
	 * 领域对象
	 * @return mixed
	 */
	public $ptype_obj;
	
	/**
	 * 构造函数
	 * @return mixed
	 */
	public function __construct()
	{
		parent::__construct();
		$this->supplier_obj = new SupplierModel(); 
		$this->country_obj = new CountryModel();
		$this->ptype_obj = new PtypeModel();
	}
	
	/**
	 * 应用领域
	 * @return mixed
	 */
    public function lists()
    {
		$supplier_type = config('supplier_type');
		$input = input('get.');
		if(isset($input['data'])){
			$data = $this->supplier_obj->getSupplierList($input);
			if(!empty($data['list']['data'])){
				foreach($data['list']['data'] as $key=>$val){
					$data['list']['data'][$key]['comp_type'] = $supplier_type[$val['comp_type']];
					if(is_numeric($val['create_time'])){
						$data['list']['data'][$key]['create_time'] = date('Y-m-d H:i:s',$val['create_time']);
					}
				}
			}
			$result = array('code'=>0,'msg'=>'','count'=>$data['list']['total'],'data'=>$data['list']['data']);
			return json($result);
		}else{
			return view('index');
		}
		return view('index');
    }
	
	/**
	 * 产品列表
	 * @return mixed
	 */
    public function product()
    {
		$ptype = $this->ptype_obj->getPtypeList(['export'=>1]);
		$psale = config('product_sale_list');
		$input = input('get.');
		$supplierData = array();
		$supplier = SupplierModel::all();
		foreach($supplier as $_s_s){
			$supplierData[$_s_s['id']] = $_s_s;
		}
		if(isset($input['data'])){
			$data = $this->supplier_obj->getSupplierProductList($input);
			if(!empty($data['list']['data'])){
				$ptype_arr = array();
				foreach($ptype['list'] as $_p){
					$ptype_arr[$_p['id']] = $_p['type_name'];
				}
				foreach($data['list']['data'] as $key=>$val){
					if(is_numeric($val['create_time'])){
						$data['list']['data'][$key]['create_time'] = date('Y-m-d H:i:s',$val['create_time']);
					}
					$data['list']['data'][$key]['type_name'] = $ptype_arr[$val['type_id']];
					$data['list']['data'][$key]['sale_name'] = $psale[$val['product_sale']];
					$data['list']['data'][$key]['comp_name'] = $supplierData[$val['supplier_id']]['comp_name'];
				}
			}
			$result = array('code'=>0,'msg'=>'','count'=>$data['list']['total'],'data'=>$data['list']['data']);
			return json($result);
		}else{
			$this->assign('supplier_data',$supplierData);
			$this->assign('ptype',$ptype['list']);
			$this->assign('psale',$psale);
			return view('product');
		}
    }
	
	/**
	 * 编辑产品
	 * @return mixed
	 */
	public function pview()
	{
		$show_file = config('show_supplier_file_dir');
		$input = input('get.');
		$product = $this->supplier_obj->getProductView($input);
		$ptype = $this->ptype_obj->getPtypeList(['export'=>1]);
		$product['base']['type_name'] = '';
		foreach($ptype['list'] as $_p){
			if($_p['id'] == $product['base']['type_id']){
				$product['base']['type_name'] = $_p['type_name'];
			}
		}
		$product['p_files']['tz'] = array('TzId'=>0,'TzName'=>'','TzUrl'=>'','TzAurl'=>'');
		$product['p_files']['az'] = array('AzId'=>0,'AzName'=>'','AzUrl'=>'','AzAurl'=>'');
		if(!empty($product['files'])){
			foreach($product['files'] as $key=>$file){
				if($file['file_type'] == 1){
					$product['p_files']['tz']['TzId'] = $file['id'];
					$product['p_files']['tz']['TzName'] = $file['file_name'];
					$product['p_files']['tz']['TzUrl'] = $file['file_url'];
					$product['p_files']['tz']['TzAurl'] = $show_file.$file['file_url'];
					unset($product['files'][$key]);
				}elseif($file['file_type'] == 2){
					$product['p_files']['az']['AzId'] = $file['id'];
					$product['p_files']['az']['AzName'] = $file['file_name'];
					$product['p_files']['az']['AzUrl'] = $file['file_url'];
					$product['p_files']['az']['AzAurl'] = $show_file.$file['file_url'];
					unset($product['files'][$key]);
				}else{
					$product['files'][$key]['file_all_url'] = $show_file.$file['file_url'];
				}
			}
		}
		$files_count = count($product['files']);
		if($files_count < 6){
			$num = 6 - $files_count;
			for($i=0;$i<$num;$i++){
				array_push(
						$product['files'],
						array(
							'id'=>0,
							'product_id'=>$input['id'],
							'file_name'=>'',
							'file_url'=>'',
							'file_type'=>0,
							'file_status'=>0,
							'file_all_url'=>'',
							'file_sort'=>0,
							'create_time'=>0,
							'update_time'=>0
						)	
					);
			}
		}
		$where = array();
		$where['type_id'] = $product['base']['type_id'];
		$size = $this->ptype_obj->getSizeByType($where);
		$size_config = config('size_list');
		$size_list = array();
		foreach($size_config as $_k=>$_s){
			if($size['size_'.strtolower($_s['code'])] == 1){
				array_push(
					$size_list,
					array(
						'code'=>$_s['code'],
						'name'=>$_s['name'],
						'input'=>'size_'.strtolower($_s['code']),
						'value'=>$product['sizes']['size_'.strtolower($_s['code'])]
					)
				);
			}
		}
		$exchange_list = $product['exchange'];
		$mator_list = array();
		if(!empty($product['mator'])){
			$fieldData = array();
			$field = FieldModel::all();
			foreach($field as $_f){
				$fieldData[$_f['id']] = $_f;
			}
			$countryData = array();
			$country = CountryModel::all();
			foreach($country as $_c){
				$countryData[$_c['id']] = $_c;
			}
			$makeData = array();
			$make = MakeModel::all();
			foreach($make as $_m){
				$makeData[$_m['id']] = $_m;
			}
			$seriesData = array();
			$series = SeriesModel::all();
			foreach($series as $_s){
				$seriesData[$_s['id']] = $_s;
			}
			$where = array();
			$mator_ids = array();
			foreach($product['mator'] as $mator){
				array_push($mator_ids,$mator['mator_id']);
			}
			$where['mator_ids'] = $mator_ids;
			$data = $this->mator_obj->getMators($where);
			if(!empty($data)){
				foreach($data as $key=>$val){
					$data[$key]['field_country_code'] = $fieldData[$val['field_id']]['field_code'].'-'.$countryData[$val['country_id']]['country_code'];
					$data[$key]['field_country_name'] = $fieldData[$val['field_id']]['field_name'].'-'.$countryData[$val['country_id']]['country_name'];
					$data[$key]['make_name'] = $makeData[$val['make_id']]['make_name'];
					$data[$key]['series_name'] = $seriesData[$val['series_id']]['series_name'];
					$data[$key]['year_name'] = '';
					if($seriesData[$val['series_id']]['from_time'] > 0){
						$data[$key]['year_name'] = date('Y/m',$seriesData[$val['series_id']]['from_time']).'~'.date('Y/m',$seriesData[$val['series_id']]['to_time']);
					}
				}
			}
			$mator_list = $data;
		}
		$psale = config('product_sale_list');
		$this->assign('ptype',$ptype['list']);
		$this->assign('psale',$psale);
		$this->assign('view', $product);
		$this->assign('size_list', $size_list);
		$this->assign('exchange_list', $exchange_list);
		$this->assign('mator_list', $mator_list);
		return view('pview');
	}
	
	/**
	 * 应用领域
	 * @return mixed
	 */
    public function mator()
    {
		$fieldData = array();
		$field = FieldModel::all();
		foreach($field as $_f){
			$fieldData[$_f['id']] = $_f;
		}
		$countryData = array();
		$country = CountryModel::all();
		foreach($country as $_c){
			$countryData[$_c['id']] = $_c;
		}
		$makeData = array();
		$make = MakeModel::all();
		foreach($make as $_m){
			$makeData[$_m['id']] = $_m;
		}
		$seriesData = array();
		$series = SeriesModel::all();
		foreach($series as $_s){
			$seriesData[$_s['id']] = $_s;
		}
		$supplierData = array();
		$supplier = SupplierModel::all();
		foreach($supplier as $_s_s){
			$supplierData[$_s_s['id']] = $_s_s;
		}
		$input = input('get.');
		if(isset($input['data'])){
			$data = $this->supplier_obj->getSupplierMatorList($input);
			if(!empty($data['list']['data'])){
				foreach($data['list']['data'] as $key=>$val){
					$data['list']['data'][$key]['field_country_code'] = $fieldData[$val['field_id']]['field_code'].'-'.$countryData[$val['country_id']]['country_code'];
					$data['list']['data'][$key]['field_country_name'] = $fieldData[$val['field_id']]['field_name'].'-'.$countryData[$val['country_id']]['country_name'];
					$data['list']['data'][$key]['make_name'] = $makeData[$val['make_id']]['make_name'];
					$data['list']['data'][$key]['series_name'] = $seriesData[$val['series_id']]['series_name'];
					$data['list']['data'][$key]['comp_name'] = $supplierData[$val['supplier_id']]['comp_name'];
					$data['list']['data'][$key]['year_name'] = '';
					if($seriesData[$val['series_id']]['from_time'] > 0){
						$data['list']['data'][$key]['year_name'] = date('Y/m',$seriesData[$val['series_id']]['from_time']).'~'.date('Y/m',$seriesData[$val['series_id']]['to_time']);
					}
					if(is_numeric($val['create_time'])){
						$data['list']['data'][$key]['create_time'] = date('Y-m-d H:i:s',$val['create_time']);
					}
				}
			}
			$result = array('code'=>0,'msg'=>'','count'=>$data['list']['total'],'data'=>$data['list']['data']);
			return json($result);
		}else{
			$where = array();
			$where['export'] = 1;
			$data = $this->country_obj->getFieldCountryList($where);
			if(!empty($data['list'])){
				foreach($data['list'] as $key=>$val){
					$fieldObj = FieldModel::get($val['field_id']);
					$countryObj = CountryModel::get($val['country_id']);
					$data['list'][$key]['field_country_code'] = $fieldObj->field_code." - ".$countryObj->country_code;
					$data['list'][$key]['field_country_name'] = $fieldObj->field_name." - ".$countryObj->country_name;
				}
			}
			$this->assign('supplier_data',$supplierData);
			$this->assign('field_country',$data['list']);
			$this->assign('field', $fieldData);
			return view('mator');
		}
    }
	
	/**
	 * 新增领域页面
	 * @return mixed
	 */
	public function aview()
	{
		$input = input('get.');
		$view = $this->supplier_obj->getSupplier($input);
		$supplier = SupplierModel::get($view['supplier_id']);
		$field = FieldModel::get($view['field_id']);
		$country = CountryModel::get($view['country_id']);
		$view['comp_name'] = $supplier['comp_name'];
		$view['field_country_code'] = $field['field_code'].'-'.$country['country_code'];
		$view['field_country_name'] = $field['field_name'].'-'.$country['country_name'];
		$this->assign('view', $view);
		return view('aview');
	}
	
	/**
	 * 新增领域页面
	 * @return mixed
	 */
	public function add()
	{
		$city_list = Session::get('city_list');
		if(empty($city_list)){
			$api_url = config('api_url.url');
			$params = array();
			$params['PageSize'] = config('paginate.list_rows');
			$ret = FiltrsHttpJson($api_url.'/v1.0/city/lists','/v1.0/city/lists','1.0',$params);
			$city_list = $ret['ResponseData']['List'];
			Session::set('city_list',$city_list);
		}
		$pro_list = array();
		foreach($city_list as $_c){
			array_push($pro_list,array('id'=>$_c['id'],'name'=>$_c['name']));
		}
		$where = array();
		$where['export'] = 1;
		$field_country = $this->country_obj->getFieldCountryList($where);
		$fieldData = array();
		$field = FieldModel::all();
		foreach($field as $_f){
			$fieldData[$_f['id']] = $_f;
		}
		$countryData = array();
		$country = CountryModel::all();
		foreach($country as $_c){
			$countryData[$_c['id']] = $_c;
		}
		foreach($field_country['list'] as $_k=>$_v){
			$field_country['list'][$_k]['name'] = $fieldData[$_v['field_id']]['field_name'].' - '.$countryData[$_v['country_id']]['country_name'];
		}
		$this->assign('f_c_list', $field_country['list']);
		$this->assign('pro_list', $pro_list);
		return view('add');
	}
	/**
	 * 编辑领域
	 * @return mixed
	 */
	public function edit()
	{
		$input = input('get.');
		$view = SupplierModel::get($input['id'])->toArray();
		$view['supplier_pass'] = authcode($view['supplier_pass']);
		$city_list = Session::get('city_list');
		if(empty($city_list)){
			$api_url = config('api_url.url');
			$params = array();
			$params['PageSize'] = config('paginate.list_rows');
			$ret = FiltrsHttpJson($api_url.'/v1.0/city/lists','/v1.0/city/lists','1.0',$params);
			$city_list = $ret['ResponseData']['List'];
			Session::set('city_list',$city_list);
		}
		$pro_list = array();
		$cities_list = array();
		$distrinct = array();
		foreach($city_list as $_c){
			array_push($pro_list,array('id'=>$_c['id'],'name'=>$_c['name']));
			if($_c['id'] == $view['pro_id']){
				foreach($_c['cities'] as $_c_c){
					array_push($cities_list,array('id'=>$_c_c['id'],'name'=>$_c_c['name']));
					if($_c_c['id'] == $view['city_id']){
						foreach($_c_c['district'] as $_c_c_c){
							array_push($distrinct,array('id'=>$_c_c_c['id'],'name'=>$_c_c_c['name']));
						}
					}
				}
			}
		}
		$where = array();
		$where['export'] = 1;
		$field_country = $this->country_obj->getFieldCountryList($where);
		$fieldData = array();
		$field = FieldModel::all();
		foreach($field as $_f){
			$fieldData[$_f['id']] = $_f;
		}
		$countryData = array();
		$country = CountryModel::all();
		foreach($country as $_c){
			$countryData[$_c['id']] = $_c;
		}
		foreach($field_country['list'] as $_k=>$_v){
			$field_country['list'][$_k]['name'] = $fieldData[$_v['field_id']]['field_name'].' - '.$countryData[$_v['country_id']]['country_name'];
		}
		$roles_ids = array();
		$roles = SupplierRoleModel::all(['supplier_id'=>$input['id']]);
		if(!empty($roles)){
			foreach($roles as $_r){
				array_push($roles_ids,$_r['f_c_id']);
			}
		}
		$this->assign('f_c_list', $field_country['list']);
		$this->assign('pro_list', $pro_list);
		$this->assign('cities_list', $cities_list);
		$this->assign('distrinct', $distrinct);
		$this->assign('roles_ids', $roles_ids);
		$this->assign('view', $view);
		return view('edit');
	}
	
	/**
	 * 更新领域
	 * @return mixed
	 */
	public function save()
	{
		if ($this->request->isPost()) {
			$input = input('post.');
			$role_ids = '';
			$role_flag = false;
			if(isset($input['role_ids'])){
				$role_ids = $input['role_ids'];
				unset($input['role_ids']);
				$role_flag = true;
			}
			if(isset($input['supplier_pass'])){
				$input['supplier_pass'] = authcode($input['supplier_pass'],'ENCODE');
			}
			if(isset($input['id']) && intval($input['id']) > 0){
				$input['update_time'] = time();
				$flag = $this->supplier_obj->save($input,['id'=>$input['id']]);
			}else{
				$input['create_time'] = time();
				$input['update_time'] = time();
				$this->supplier_obj->data($input);
				$flag = $this->supplier_obj->save();
				$input['id'] = $this->supplier_obj->id;
			}
			if($role_flag){
				SupplierRoleModel::destroy(['supplier_id' => $input['id']]);
				if(!empty($role_ids)){
					$role_ids_arr = explode('|',$role_ids);
					foreach($role_ids_arr as $_r){
						$insert_data = array('supplier_id'=>$input['id'],'f_c_id'=>$_r,'create_time'=>time());
						SupplierRoleModel::create($insert_data);
					}
				}
			}
			if($flag){
				$result = [
					'code' => 1,
					'msg'  => '请求成功',
					'list' => $input
				];
			}else{
				$result = [
					'code' => 0,
					'msg'  => '请求失败',
					'list' => $input
				];
			}
			return json($result);
		}else{
			$this->error('更新领域数据，请求不合法！');
		}
	}	
	
	/**
	 * 删除领域
	 * @return mixed
	 */
	public function del()
	{
		if ($this->request->isPost()) {
			$input = input('post.');
			$field = SupplierModel::get($input['id']);
			$field->supplier_status = -1;
			$flag = $field->save();
			if($flag){
				$result = [
					'code' => 1,
					'msg'  => '请求成功',
					'list' => $input
				];
			}else{
				$result = [
					'code' => 0,
					'msg'  => '请求失败',
					'list' => $input
				];
			}
			return json($result);
		}else{
			$this->error('更新领域数据，请求不合法！');
		}
	}
	
	/**
	 * 更新领域状态
	 * @return mixed
	 */
	public function setStatus()
	{
		if ($this->request->isPost()) {
			$input = input('post.');
			$field = SupplierModel::get($input['id']);
			$status = 1;
			if($field->supplier_status){
				$status = 0;
			}
			$field->supplier_status = $status;
			$flag = $field->save();
			if($flag){
				$result = [
					'code' => 1,
					'msg'  => '请求成功',
					'list' => $input
				];
			}else{
				$result = [
					'code' => 0,
					'msg'  => '请求失败',
					'list' => $input
				];
			}
			return json($result);
		}else{
			$this->error('更新领域数据，请求不合法！');
		}
	}
	
	/**
	 * 更新领域状态
	 * @return mixed
	 */
	public function pass()
	{
		if ($this->request->isPost()) {
			$input = input('post.');
			$flag = $this->supplier_obj->updateStatus($input);
			if($flag){
				$result = [
					'code' => 1,
					'msg'  => '请求成功',
					'list' => $input
				];
			}else{
				$result = [
					'code' => 0,
					'msg'  => '请求失败',
					'list' => $input
				];
			}
			return json($result);
		}else{
			$this->error('更新领域数据，请求不合法！');
		}
	}
}
